function view(obj){
	var form = document.getElementById('ViewForm');
	form.item.value = obj.id;
	//alert(obj.id);
	form.submit();	
}

function search(){
	var search = document.getElementById('search');
	window.location.href='search.php?search='+search.value;

}

function addToCart(id){
//	alert(id);
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    document.getElementById("response").innerHTML=xmlhttp.responseText;
	    }
	}
	param="type=add&item="+id;
	//alert(param);
	xmlhttp.open("POST","updatecart.php",true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(param);
	$('#response').fadeIn(2000).fadeOut(2000);
}

function removeFromCart(id){
	//document.getElementById("tr"+id).style.display="none";
	obj = "#tr"+id;
	$(obj).fadeOut(500);

	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    document.getElementById("totalprice").innerHTML=xmlhttp.responseText;
	    }
	}
	param="type=remove&item="+id;
	//alert(param);
	xmlhttp.open("POST","updatecart.php",true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(param);
}

function update(quantity, item){
	//alert(quantity+","+item);
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    document.getElementById("totalprice").innerHTML=xmlhttp.responseText;
	    }
	}
	param="type=update&quantity="+quantity+"&itemID="+item;
	//alert(param);
	xmlhttp.open("POST","updatecart.php",true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(param);
}


function validateRegisterForm(type){
	var msg='';
	if(type==1){
		if($("#username").val().length<=3)
			msg= msg+'Very Short Username\n';
		if($("#password").val().length<=5)
			msg= msg+'Very Short Password\n';
		if($("#password").val() != $("#verify").val())
			msg= msg+'Password and Confirm Password doesn\'t match\n';
	}
	if($("#first").val().length<=0)
		msg= msg+'Enter First Name\n';
	if($("#last").val().length<=0)
		msg= msg+'Enter Last Name\n';
	if($("#email").val().length<=0)
		msg= msg+'Enter Phone Number\n';
	if($("#phone").val().length<=0)
		msg= msg+'Enter Phone Number\n';
	if($("#street1").val().length<=0)
		msg= msg+'Enter Stree1\n';
	if($("#city").val().length<=0)
		msg= msg+'Enter City\n';
	if($("#province").val().length<=0)
		msg= msg+'Enter Province\n';
	if($("#country").val().length<=0)
		msg= msg+'Enter Country\n';
	if($("#postalcode").val().length<=0)
		msg= msg+'Enter Postal Code\n';
	
	
	/*if($("#checkbox")=checked)
		msg= msg+'Please check terms and condtions\n';
	*/
	if(msg!=''){
		alert(msg);
		return false;
	}
		
	return true;

}

function isSpecial(evt){
	 var charCode = (evt.which) ? evt.which : event.keyCode
	 if (charCode == 8 ||  (charCode==32)|| (charCode>=37 && charCode<=40))
	    return true;
	
	 return false;	
}

function isNumberKey(evt)
{
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode >= 48 && charCode <= 57)
    return true;

 return false;
}

function isValid(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
 	if ((charCode >= 48 && charCode <= 57) || (charCode>=65 && charCode<=90) || (charCode>=97 && charCode<=122))
   		return true;
 	return false;

}

function isAlphabet(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
 	if ((charCode>=65 && charCode<=90) || (charCode>=97 && charCode<=122))
   		return true;
 	return false;
}

function isSpace(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
 	if (charCode==32)
   		return true;
 	return false;
}

function updateorder(obj,type)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	   // document.getElementById("totalprice").innerHTML=xmlhttp.responseText;
	    }
	}
	param="type="+type+"&id="+obj.id;
	//alert(param);
	xmlhttp.open("POST","updateorder.php",true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(param);
	location.reload();

}

function submitCheckout(){
    var msg='';

//billing address form validation
	if($("#BillingFirstName").val().length<=0)
		msg= msg+'Enter First Name in the billing address form\n';
	if($("#BillingLastName").val().length<=0)
		msg= msg+'Enter Last Name in the billing address form\n';
	if($("#BillingAddress1").val().length<=0)
		msg= msg+'Billing address field can not be empty\n';
	if($("#BillingCity").val().length<=0)
		msg= msg+'Enter city in the billing address form\n';
	if($("#BillingProvince").val().length<=0)
		msg= msg+'Please select province in the billing address form\n';
	if($("#BillingPostalCode").val().length<=0)
		msg= msg+'Enter Postal code in the billing address form\n';

//shipping address form validation
    if($("#ShipFirstName").val().length<=0)
        msg= msg+'Enter First Name in the shipping address form\n';
    if($("#ShipLastName").val().length<=0)
        msg= msg+'Enter Last Name in the shipping address form\n';
    if($("#ShipAddress1").val().length<=0)
        msg= msg+'Shipping address field can not be empty\n';
    if($("#ShipCity").val().length<=0)
        msg= msg+'Enter city in the shipping address form\n';
    if($("#ShipProvince").val().length<=0)
        msg= msg+'Please select province in the shipping address form\n';
    if($("#ShipPostalCode").val().length<=0)
        msg= msg+'Enter Postal code in the shipping address form\n';


	if($("#CreditCardNumber").val().length<12)
		msg= msg+'Invalid credit card number\n';
	if($("#CardHoldersName").val().length<=0)
		msg= msg+'Please enter card holders name\n';
	if($("#CC_ExpDate_Month").val()==0)
		msg= msg+'Please select expiry month\n';
    if($("#CC_ExpDate_Year").val()==0)
        msg= msg+'Please select expiry year\n';
	
	

	if(msg!=''){
		alert(msg);
		return false;
	}
    else{
        document.CheckOutForm.submit();
    }
}
//Timeout function

function InitSessionTimer() {
    warn_sec =  15 * 1000;             //Warning time in milliseconds
    timeout_sec = 60 * 1000;          //Actual timeout in milliseconds
    show_warning = true;
    epoch = new Date().getTime();
    CheckSessionStatus();
}

function CheckSessionStatus() {
    
    //Check for session warning
    epoch2 = new Date().getTime();
    if (epoch2 > epoch + warn_sec && epoch2 < epoch + timeout_sec && show_warning) {
        show_warning = false; //Don't show again
        alert_shown = true;
        alert("Your session will timeout in " + Math.round((timeout_sec - warn_sec) / 60000) + " minute, please click a button or navigate to another page to refresh your session before it expires.");
        down = setTimeout("CheckSessionStatus();", 1000);
    } else if (epoch2 > epoch + timeout_sec) {
        alert("Your session has timed out.");
        window.location.href = 'logout.php';
    } else {
        down = setTimeout("CheckSessionStatus();", 1000);
    }
}
